package gazzera.node.core.plugins.shareplugin;

import gazzera.utils.*;

import java.util.*;


import com.sleepycat.je.*;
import com.sleepycat.persist.*;

@Deprecated
public class FileDb
{
	    private EntityStore store;

	    int lastKey = 0;
	    
	    private PrimaryIndex<Integer,FileDesc> fileById;
	    private SecondaryIndex<Integer,Integer,FileDesc> filesByParent;
	    private SecondaryIndex<Integer,Integer,FileDesc> filesByShare;
	    private SecondaryIndex<Hash,Integer,FileDesc> filesByHash;
	   
	    
	    public FileDb(Environment env) {
	
	        StoreConfig storeConfig = new StoreConfig();

	        storeConfig.setReadOnly(env.getConfig().getReadOnly());
	        storeConfig.setAllowCreate(!env.getConfig().getReadOnly());

	        store = new EntityStore(env, "FileStore", storeConfig);

	    	fileById = store.getPrimaryIndex(
	    			Integer.class, FileDesc.class);

	        filesByParent = store.getSecondaryIndex(
	    			fileById, Integer.class, "parentId");

	        filesByShare = store.getSecondaryIndex(
	    			fileById, Integer.class, "shareId");
	        
	        filesByHash = store.getSecondaryIndex(
	    			fileById, Hash.class, "hash");
	    	
	    	FileDesc x = new FileDesc();
	    	x.id = 0;
	    	x.parentId = 0;
	    	x.directory = true;
	    	x.filename = "ROOT";
	    	fileById.put(x);
	    	
	        SortedMap<Integer, FileDesc> k = fileById.sortedMap(); 
	        if (k.size() == 0)
	        {
	        	lastKey = 1;
	        }
	        else
	        {
	        	lastKey = k.lastKey();
	        }
	    }

	    public FileDesc getFileByHash(Hash hash)
	    {
	    	return filesByHash.get(hash);
	    }
	   
	    public EntityStore getEntityStore() {
	        return store;
	    }

	    public void close() {
	        if (store != null) {
	            try {
	                store.close();
	            } catch(DatabaseException dbe) {
	                System.err.println("Error closing store: " +
	                                    dbe.toString());
	               System.exit(-1);
	            }
	        }
	    }
	    
	    public synchronized int nextKey()
	    {
	    	return ++lastKey;
	    }
	    
	    public FileDesc findId(int parent, String name)
	    {
	    	EntityCursor<FileDesc> c = filesByParent.subIndex(parent).entities();
	    	try
			{
				for (FileDesc f : c)
				{
					if (f.filename.equals(name))
					{
						return f;
					}
				}
				return null;
			}
			finally
			{
				c.close();
			}
	    	
	    }
	    public boolean delete(int id)
	    {
	    	return fileById.delete(id);
	    }
	    public void put(FileDesc f)
	    {
	    	fileById.put(f);
	    }
	    public FileDesc get(int id)
	    {
	    	return fileById.get(id);
	    }
	   
	    public EntityCursor<FileDesc> children(Integer parentId)
	    {
	    	return filesByParent.subIndex(parentId).entities();
	    }
	    public EntityCursor<FileDesc> filesByHash(Hash hash)
	    {
	    	return filesByHash.subIndex(hash).entities();
	    } 
	    public EntityCursor<FileDesc> filesByShare(int shareid)
	    {
	    	return filesByShare.subIndex(shareid).entities();
	    }
	    public FileDesc find(Hash hash)
	    {
	    	EntityCursor<FileDesc> c = filesByHash(hash);
			try
			{
				FileDesc d;
				while ((d=c.next()) != null)
				{
					return d;
				}
			}
			finally
			{
				c.close();
			}
			return null;
	    }
	    public void setHash(int id, Hash hash)
	    {
	    	FileDesc d = get(id);
	    	d.hash = hash;
	    	put(d);
	    }
	    
	    public void show(int parent, String ident)
	    {
	    	EntityCursor<FileDesc> c = filesByParent.subIndex(parent).entities();
			try
			{
				for(FileDesc k : c)
				{
					if(k.id!=0)
					{
						System.out.println(ident+k.filename+" ("+k.id+")");
						show(k.id, ident+"  ");
					}
				}
			}
			finally
			{
				c.close();
			}
	    }
	    
	    public void flush()
	    {
		store.sync();
	    }
}
